<?php
namespace app\api\controller\wdsxh\mall;
use app\admin\model\wdsxh\mall\Logistics;
use app\common\controller\Api;

use think\Db;
use think\Exception;

class Refund extends Api
{
    protected $noNeedLogin = ['payResult'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $model = '';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = (new \app\admin\model\wdsxh\mall\Refund());
    }



    //申请退款
    //param:token id
    public function refund()
    {
        $param = $this->request->param();
        $user_id = $this->auth->id;
        $orderModel = (new \app\api\model\wdsxh\mall\Order())->where('id', $param['order_id'])->find();
        $refund_reason = '退款原因:' . $param['refund_reason'] . ',' . '退款详情:' . $param['refund_content'];
        $edit = (new \app\api\model\wdsxh\mall\Order())
            ->where('id', $param['order_id'])
            ->update([
                'refund_status' => '1',
                'status' => '-1'
            ]);
        if ($edit) {
            (new \app\admin\model\wdsxh\mall\Refund())->insert([
                'order_id' => $orderModel['id'],//订单id
                'refund_price' => $orderModel['pay_price'],//退款金额
                'refund_reason' => $refund_reason,//退款用户说明
                'status' => $orderModel['status'],//退款前状态
                'user_id' => $user_id,//用户id
                'createtime' => time(),//创建时间
                'refund_time' => time(),//退款时间
            ]);
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }
    }

        //取消退款
        //param:token id
        public function cancel_refund()
    {
        $param = $this->request->param();
        $user_id = $this->auth->id;
        $orderModel = (new \app\admin\model\wdsxh\mall\Refund())
            ->where('order_id', $param['order_id'])->where('user_id',$user_id)
            ->find();
        $edit = (new \app\api\model\wdsxh\mall\Order())->allowField(true)
            ->where('id', $param['order_id'])
            ->update([
                'refund_status' => '0',
                'status' => $orderModel['status'],
            ]);
        if ($edit) {
            (new \app\admin\model\wdsxh\mall\Refund())
                ->where('order_id',$param['order_id'])
                ->where('user_id',$user_id)->delete();
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    //添加快递单号
    public function receipt(){
        $param = $this->request->param();
        Db::startTrans();
        try{
            (new \app\admin\model\wdsxh\mall\Refund())->allowField(true)
                ->where('order_id',$param['order_id'])
                ->update([
                    'refund_express_no' =>$param['refund_express_no'],//快递单号
                    'add_express_no_time' => time(),//添加单号时间
                ]);
          (new \app\api\model\wdsxh\mall\Order())->allowField(true)
                ->where('id', $param['order_id'])
                ->update([
                    'refund_status' => '3',
                ]);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('请求成功');

    }
}



